﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;

namespace SheXiangTou.Tools
{
    class PictureTool
    {
        public static string Base64ToJPG(string pictureStr, string cardNum, string saveUrl)
        {
            string RES = string.Empty;
            //将Base64String转为图片并保存
            try
            {
                string xps = pictureStr.Split("base64,")[1];
                byte[] arr2 = Convert.FromBase64String(pictureStr.Split("base64,")[1]);
                using (MemoryStream ms2 = new MemoryStream(arr2))
                {
                    Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
                    int abc = rand.Next();
                    Bitmap bmp2 = new Bitmap(ms2);
                    bmp2.Save(Path.Combine(saveUrl, abc + "_" + cardNum + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                    RES = Path.Combine(saveUrl, abc + "_" + cardNum + ".jpg");
                }
            }
            catch
            {
            }
            return RES;
        }
        /// <summary>
        /// 把图片转换成文字
        /// </summary>
        /// <param name="saveUrl"></param>
        /// <returns></returns>
        public static string JPGToBase64(string saveUrl)
        {
            string RES = string.Empty;
            //读图片转为Base64String
            try
            {

                Bitmap bmp1 = new Bitmap(saveUrl);
                using (MemoryStream ms1 = new MemoryStream())
                {
                    bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] arr1 = new byte[ms1.Length];
                    ms1.Position = 0;
                    ms1.Read(arr1, 0, (int)ms1.Length);
                    ms1.Close();
                    RES = Convert.ToBase64String(arr1);
                }
            }
            catch
            {
            }
            return RES;
        }
    }
}
